<?php
//------------------------
// 前台商品控制器
//-------------------------

namespace app\home\controller;

use app\common\controller\HomeBase;
use app\common\model\Goods as GoodsModel;
use app\common\model\GoodsAlbum as GoodsAlbumModel;
use app\common\model\GoodsSpec as GoodsSpecModel;
use think\Db;
use think\Session;
use think\Cookie;


class Goods extends HomeBase
{
	protected $GoodsModel;
	protected $GoodsAlbumModel;
	protected $GoodsTypeAttrViewModel;
	protected $GoodsSpecModel;

	protected function _initialize()
    {
        parent::_initialize();
        $this->GoodsModel              = new GoodsModel();
        $this->GoodsAlbumModel         = new GoodsAlbumModel();
        $this->GoodsSpecModel          = new GoodsSpecModel();
    }



    // 商品详情页
	public function goodsDetails(){
		$goods_id = $this->request->param('id',0,'intval');
// echo Cookie::get('cart');die;
		// if (!$goods_id) {
		// 	$this->error('非法操作');
		// }

		// 将当前商品存入到最近浏览cookie中
		$recentDisplay = Cookie::has('recentDisplay') ? unserialize(Cookie::get('recentDisplay')) : array();

    	// 把刚刚浏览的这件商品的ID放到这个数组中的最前面
    	array_unshift($recentDisplay, $goods_id);
    	// 去重复
    	$recentDisplay = array_unique($recentDisplay);
    	// 如果超过10个就只保留前10个
    	if(count($recentDisplay) > 10) {
    		$recentDisplay = array_slice($recentDisplay, 0, 10);
    	}
    	// 把处理好的数组保存回COOKIE中
    	$recentDisplay = serialize($recentDisplay);
    	Cookie::set('recentDisplay', $recentDisplay, 30 * 86400);

		// 商品信息
		$goodsInfo = $this->GoodsModel->where('id',$goods_id)->find()->toArray();
		$this->assign('goodsInfo',$goodsInfo);

		// 商品规格信息
	    $filter_spec = $this->GoodsSpecModel->getSpec($goods_id);
		$this->assign('filter_spec',$filter_spec);//规格参数

		$spec_goods_price = $this->GoodsSpecModel->getGoodsSpecPrice($goods_id);

		$this->assign('spec_goods_price', $spec_goods_price); // 规格 对应 价格

		// 商品相册
		$goodsAlbumInfo = $this->GoodsAlbumModel->where('goods_id',$goods_id)->select()->toArray();
		$this->assign('goodsAlbumInfo',$goodsAlbumInfo);

		//可选属性
		$goodsAttrInfo1 = Db::table('platform_goods_attr')
		->join('platform_type_attr','platform_goods_attr.attr_id = platform_type_attr.id')
		->where('platform_goods_attr.goods_id',$goods_id)
		->where('platform_type_attr.is_select',1)
		->select();

		$_goodsAttrInfo1 = array(); 
		foreach ($goodsAttrInfo1 as $k => $v){
			$_goodsAttrInfo1[$v['attr_name']][] = $v;
		}
		$this->assign('_goodsAttrInfo1',$_goodsAttrInfo1);

		// 唯一属性
		$goodsAttrInfo2 = Db::table('platform_goods_attr')
		->join('platform_type_attr','platform_goods_attr.attr_id = platform_type_attr.id')
		->where('platform_goods_attr.goods_id',$goods_id)
		->where('platform_type_attr.is_select',0)
		->select()->toArray();
		$this->assign('goodsAttrInfo2',$goodsAttrInfo2);

		return $this->fetch('goods_details');
	}

	// ajax 动态获取商品价格
	public function ajaxGetGoodsPrice(){
		$old_price    = $this->request->param('price',0);
		$goods_id = $this->request->param('goods_id',0);

		$user_price = $this->GoodsModel->calculationPrice($goods_id,$old_price);

		$returnData = array(
				'user_price' => $user_price
			);

		$this->success($returnData);
	}




}
